﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSL_Tools.XMLTools;
using System.Xml;

namespace ReportingAppConfig
{
    [Serializable]
    public class ApplicationProperties
    {
        #region -- Public Properties ----------------------------------------------------
        #region -- Analysis Settings ----------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("Minimum Points For Constant Interval")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Analysis Settings")]
        [System.ComponentModel.DefaultValue(10)]
        public int minimumPointsForConstantInterval { get; set; }

        [System.ComponentModel.DisplayName("Minimum Seconds For Constant Interval")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Analysis Settings")]
        [System.ComponentModel.DefaultValue(60)]
        public int minimumSecondsForConstantInterval { get; set; }

        [System.ComponentModel.DisplayName("Ignore User Name From Analysis Notes")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Analysis Settings")]
        [System.ComponentModel.DefaultValue(true)]
        public bool IgnoreUserNameFromAnalysisNotes { get; set; }

        [System.ComponentModel.DisplayName("Use Detailed Timings For Tables When Available")]
        [System.ComponentModel.Description("If true, this setting enables the ability to choose whether to recalculate timing information based on pass/fail and warmup/cooldown, etc. NOTE: This will only work if the selected test run contains detailed timing information.")]
        [System.ComponentModel.Category("Analysis Settings")]
        [System.ComponentModel.DefaultValue(true)]
        public bool UseDetailedTimingsForTablesWhenAvailable { get; set; }
        #endregion

        #region -- Chart Settings -------------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("Chart Template Location")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Chart Settings")]
        [System.ComponentModel.DefaultValue("<MyDocuments>\\Load Test Reports\\Chart Templates\\")]
        public string ChartTemplateLocation { get; set; }

        [System.ComponentModel.DisplayName("Chart Template Name")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Chart Settings")]
        [System.ComponentModel.DefaultValue("ChartTemplate.xml")]
        public string ChartTemplateName { get; set; }

        [System.ComponentModel.DisplayName("Max Machines To Chart")]
        [System.ComponentModel.Description("The maximum number of machines to buikd into a chart without prompting for user interaction")]
        [System.ComponentModel.Category("Chart Settings")]
        [System.ComponentModel.DefaultValue(true)]
        public int maxMachinesToChartWithoutPrompting { get; set; }

        [System.ComponentModel.DisplayName("Chart Object Location")]
        [System.ComponentModel.Description("The XML file that contains the default list of charting objects to use")]
        [System.ComponentModel.Category("Chart Settings")]
        [System.ComponentModel.DefaultValue("DefaultChartingObjects.xml")]
        public string ChartObjectFileName { get; set; }

        [System.ComponentModel.DisplayName("Charting: Legend Row Height")]
        [System.ComponentModel.Description("The height, in pixels, that a row in the chart legend will use. This allows the vertical height of the chart to properly scale for the loegend entries")]
        [System.ComponentModel.DefaultValue(48)]
        [System.ComponentModel.Category("Chart Settings")]
        public int Charting_LegendRowHeight { get; set; }

        [System.ComponentModel.DisplayName("Chart Template With Legend Name")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("Templates\\ChartTemplateWithLegend.xml")]
        public string ChartTemplateWithLegendName { get; set; }
        #endregion

        #region -- Core Settings --------------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("Current Version")]
        [System.ComponentModel.Description("The version of the application installed on this machine. It is used to verify that the correct version of code is also on the SQL database holding the results")]
        [System.ComponentModel.Category("Core Settings")]
        [System.ComponentModel.DefaultValue("5.1.0 Beta")]
        public string CurrentVersion { get; set; }

        [System.ComponentModel.DisplayName("Folder To Store Results")]
        [System.ComponentModel.Description("The folder where test and report results will get saved. NOTE: Use '<MyDocuments>' at the beginning if you want the path to be relative to the built in My Documents folder")]
        [System.ComponentModel.Category("Core Settings")]
        [System.ComponentModel.DefaultValue("<MyDocuments>\\Load Test Reports")]
        public string FolderToStoreResults { get; set; }
        #endregion

        #region -- Database Settings ----------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("SQL Connection String")]
        [System.ComponentModel.Description("The string used to connect to the LoadTest SQL Server database")]
        [System.ComponentModel.Category("Database Settings")]
        [System.ComponentModel.DefaultValue("Data Source=.\\;Integrated Security=SSPI;Initial Catalog=LoadTest2010")]
        public string sqlConn { get; set; }

        [System.ComponentModel.DisplayName("Connection Timeout")]
        [System.ComponentModel.Description("The timeout value (in seconds) for attempting to establish a SQL Connection")]
        [System.ComponentModel.Category("Database Settings")]
        [System.ComponentModel.DefaultValue(60)]
        public int ConnectionTimeout { get; set; }

        [System.ComponentModel.DisplayName("Command Timeout")]
        [System.ComponentModel.Description("The timeout value (in seconds) for attempting to complete a SQL command")]
        [System.ComponentModel.Category("Database Settings")]
        [System.ComponentModel.DefaultValue(300)]
        public int CommandTimeout { get; set; }

        [System.ComponentModel.DisplayName("LoadTest Database Name")]
        [System.ComponentModel.Description("The name of the LoadTest database. By default, Visual Studio builds the database as 'LoadTest2010'")]
        [System.ComponentModel.Category("Database Settings")]
        [System.ComponentModel.DefaultValue("Loadtest2010")]
        public string LoadTestDatabaseName { get; set; }
        #endregion

        #region -- Report Settings ------------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("Default Report Name")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Report Settings")]
        [System.ComponentModel.DefaultValue("Run XXX TestReport.docx")]
        public string DefaultReportName { get; set; }

        [System.ComponentModel.DisplayName("Number Top Slowest")]
        [System.ComponentModel.Description("The number of rows to report the top slowest tests, transactions, pages, etc.")]
        [System.ComponentModel.Category("Report Settings")]
        [System.ComponentModel.DefaultValue(10)]
        public int numTopSlowest { get; set; }

        [System.ComponentModel.DisplayName("Use Detailed Timings For Report When Available")]
        [System.ComponentModel.Description("If true, the report timings shown for Test, Transaction and Page will be recalculated using the detailed timing info. Otherwise, all timing charts and tables will be built using the summary data stored in the DB")]
        [System.ComponentModel.Category("Report Settings")]
        [System.ComponentModel.DefaultValue(true)]
        public bool UseDetailedTimingsForReportWhenAvailable { get; set; }

        [System.ComponentModel.DisplayName("Order 'GroupedByName' Timings Tables by Avg Time")]
        [System.ComponentModel.Description("Orders the tables based on Avg Time (slowest to Fastest) if True, or by the Name if False")]
        [System.ComponentModel.Category("Report Settings")]
        [System.ComponentModel.DefaultValue(true)]
        public bool order_GROUPEDBYNAME_Timings_ByAvgTime { get; set; }
        #endregion

        #region -- Template Settings ----------------------------------------------------------------------------------------
        [System.ComponentModel.DisplayName("Scenario Template Location")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("\\Templates\\01_ScenarioTemplate.docx")]
        public string ScenarioTemplateLocation { get; set; }

        [System.ComponentModel.DisplayName("LoadTest Properties Template Loacation")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("\\Templates\\01_LoadTestPropertiesTemplate.docx")]
        public string LoadTestPropertiesTemplateLoacation { get; set; }

        [System.ComponentModel.DisplayName("Database Inforamtion Template Location")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("Templates\\DatabaseInfoTemplate.xaml")]
        public string DatabaseInfoTemplateLocation { get; set; }

        [System.ComponentModel.DisplayName("Database File Inforamtion Template Location")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("Templates\\DatabaseFileInfoLineTemplate.xaml")]
        public string DatabaseFileInfoTemplateLocation { get; set; }

        [System.ComponentModel.DisplayName("Database Top Tables Template Location")]
        [System.ComponentModel.Description("")]
        [System.ComponentModel.Category("Template Settings")]
        [System.ComponentModel.DefaultValue("Templates\\DatabaseInfoTopTablesTemplate.xaml")]
        public string DatabaseInfoTopTablesTemplateLocation { get; set; }
        #endregion
        #endregion

        #region -- Private Properties ---------------------------------------------------
        #endregion

        #region -- Constructors ---------------------------------------------------------
        public ApplicationProperties() { }
        #endregion

        #region -- Public Methods -------------------------------------------------------
        /// <summary>
        /// Used to load the config user settings into the  <see cref="AppConfiguration"/> class settings
        /// </summary>
        public void GetConfigValues(XmlDocument xDoc)
        {
            //xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name = \"IgnoreUserNameFromAnalysisNotes\"]")
            this.IgnoreUserNameFromAnalysisNotes = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"IgnoreUserNameFromAnalysisNotes\"]"), "Value", true);
            this.minimumPointsForConstantInterval = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"minimumPointsForConstantInterval\"]"), "Value", 10);
            this.minimumSecondsForConstantInterval = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"minimumSecondsForConstantInterval\"]"), "Value", 60);
            this.UseDetailedTimingsForTablesWhenAvailable = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"UseDetailedTimingsForTablesWhenAvailable\"]"), "Value", true);

            this.Charting_LegendRowHeight = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"Charting_LegendRowHeight\"]"), "Value", 41);
            this.ChartObjectFileName = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ChartObjectFileName\"]"), "Value", "");
            this.ChartTemplateName = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ChartTemplateName\"]"), "Value", "");
            this.maxMachinesToChartWithoutPrompting = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"maxMachinesToChartWithoutPrompting\"]"), "Value", 10);
            this.ChartTemplateLocation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ChartTemplateLocation\"]"), "Value", "");
            this.ChartTemplateWithLegendName = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ChartTemplateWithLegendName\"]"), "Value", "");

            this.CurrentVersion = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"CurrentVersion\"]"), "Value", "");
            this.FolderToStoreResults = GetFolderToStoreResults(XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"FolderToStoreResults\"]"), "Value", ""));

            this.CommandTimeout = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"CommandTimeout\"]"), "Value", 60);
            this.ConnectionTimeout = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ConnectionTimeout\"]"), "Value", 60);
            this.LoadTestDatabaseName = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"LoadTestDatabaseName\"]"), "Value", "");
            this.sqlConn = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"sqlConn\"]"), "Value", "");

            this.DefaultReportName = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"DefaultReportName\"]"), "Value", "");
            this.numTopSlowest = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"numTopSlowest\"]"), "Value", 15);
            this.order_GROUPEDBYNAME_Timings_ByAvgTime = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"order_GROUPEDBYNAME_Timings_ByAvgTime\"]"), "Value", true);
            this.UseDetailedTimingsForReportWhenAvailable = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"UseDetailedTimingsForReportWhenAvailable\"]"), "Value", true);

            this.DatabaseFileInfoTemplateLocation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"DatabaseFileInfoTemplateLocation\"]"), "Value", "");
            this.DatabaseInfoTemplateLocation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"DatabaseInfoTemplateLocation\"]"), "Value", "");
            this.DatabaseInfoTopTablesTemplateLocation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"DatabaseInfoTopTablesTemplateLocation\"]"), "Value", "");
            this.LoadTestPropertiesTemplateLoacation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"LoadTestPropertiesTemplateLocation\"]"), "Value", "");
            this.ScenarioTemplateLocation = XmlHelper.GetXmlAttributeValue(xDoc.SelectSingleNode("Configuration/ApplicationProperties/setting[@name=\"ScenarioTemplateLocation\"]"), "Value", "");

        }

        /// <summary>
        /// Used to set the <see cref="AppConfiguration"/> class settings back into the config user settings file and then to save them.
        /// </summary>
        public void SaveConfigValues()
        {
        }
        #endregion

        #region -- Private Methods ------------------------------------------------------
        private string GetFolderToStoreResults(string folderLocation)
        {
            if (folderLocation.StartsWith("<MyDocuments>"))
            {
                folderLocation = folderLocation.Replace("<MyDocuments>", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            }
            return folderLocation;
        }
        #endregion
    }
}
